package chen.web.util;

import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import chen.web.config.ConfigRepository;
import chen.web.config.SiteConfig;

public class Pagination {

	private int currentPage = 1;	//当前页面
	private int infoAmount;		//信息总数
	private int pageAmount;		//页面总数
	private int pageInfoCount = 20;	//每页信息数
	private int paginationLength = 7;	//页面列表长度
	private String baseURL;				//不含page参数值的url

	public Pagination(int totalInfo, int pageInfoCount){
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
		init(request, totalInfo, pageInfoCount);
	}
	
	/**
	 * @param request	HttpServletRequest
	 * @param infoAmount	总条目数
	 * @param pageInfoCount		每页显示条目数
	 */
	public Pagination(HttpServletRequest request, int totalInfo, int pageInfoCount){
		init(request, totalInfo, pageInfoCount);
	}
	
	private void init(HttpServletRequest request, int infoAmount, int pageInfoCount){
		//初始化页面总数
		pageAmount = (int)Math.ceil((float)infoAmount / pageInfoCount);
		if(pageAmount <= 0){
			pageAmount = 1;
		}
		//初始化当前页数
		int currentPage = getPageNumber(request);
		this.currentPage = currentPage > pageAmount ? pageAmount : currentPage < 1 ? 1 : currentPage;
		//初始化页面信息数
		this.infoAmount = infoAmount;
		if(pageInfoCount > 0){
			this.pageInfoCount = pageInfoCount;
		}
		//初始化baseURL, 不包含page参数值
		Enumeration<?> e = request.getParameterNames();
		StringBuffer uri = new StringBuffer().append(ConfigRepository.instance().get(SiteConfig.class).getBaseURL())
											.append(request.getServletPath())
											.append('?');
		while(e.hasMoreElements()){
			String name = (String)e.nextElement();
			//如果参数中有page则不计
			if(name.equals("page")){
				continue;
			}
			String value = request.getParameter(name);
			uri.append(name).append("=").append(value).append("&");
		}
		uri.append("page=");
		baseURL = uri.toString();
	}

	public String getBaseURL() {
		return baseURL;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public int getInfoAmount() {
		return infoAmount;
	}

	public int getPageInfoCount() {
		return pageInfoCount;
	}

	public int getNextPage() {
		if(currentPage < pageAmount){
			return currentPage + 1;
		} 
		return pageAmount;
	}


	public int getPrevPage() {
		if(currentPage > 1){
			return currentPage - 1;
		}
		return 1;
	}
	
	public boolean isFirstPage(){
		return currentPage == 1 ? true : false;
	}
	
	public boolean isLastPage(){
		return currentPage == pageAmount ?  true : false;
	}
	
	public int getBeginOffset(){
		return (currentPage - 1)*pageInfoCount;
	}
	
	public int getPageAmount(){
		return pageAmount;
	}
	
	public int getPaginationLength() {
		return paginationLength;
	}

	public void setPaginationLength(int paginationLength) {
		this.paginationLength = paginationLength;
	}

	public int[] getPages() {
		int[] pages = null;		//页面列表
		int n = pageAmount -currentPage ;
		if(n < paginationLength && pageAmount < paginationLength){
			pages = new int[pageAmount];
		} else {
			pages = new int[paginationLength];
		}

		int start = currentPage - paginationLength / 2;
		
		if(currentPage < paginationLength){
			start = 1;
		}  else if(start > pageAmount -paginationLength + 1){
			start = pageAmount -paginationLength + 1;
		} 
		
		for(int i=0; i<pages.length; i++){
			pages[i] = start + i;
		}

		return pages;
	}
	
	private int getPageNumber(HttpServletRequest request){
		String s_page = request.getParameter("page");
		int page = 1;
		if(s_page != null && s_page.trim().length() > 0){
			page = Integer.parseInt(s_page);
		}
		return page;
	}
}
